/*
 * @Descripttion: 
 * @version: 
 * @Author: lwj
 * @Date: 2022-06-14 18:56:19
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2023-01-31 16:08:57
 */
import Toast from '@vant/weapp/toast/toast';
var WXBizDataCrypt = require('../../utils/WXBizDataCrypt') //解密手机号
import {
  validateMobile,
  validatePwd
} from '../../utils/validate.js';
import {
  reqSmsCode,
  reqForgetPwd,
  reqWxUserInfo
} from '../../request/api.js';

Page({
  data: {
    srcMobile: '../../statics/images/login-register/mobile_icon.png',
    srcMessage: '../../statics/images/login-register/message_icon.png',
    srcPwd: '../../statics/images/login-register/pwd_icon.png',
    srcVerify: '../../statics/images/login-register/verify_icon.png',
    srcEyeClose: '../../statics/images/login-register/eye_close_icon.png',
    srcEyeOpen: '../../statics/images/login-register/eye_open_icon.png',
    mobile: '',
    smsCode: '',
    pwd1: '',
    pwd2: '',
    isPassword1: true,
    isPassword2: true,
    smsCodeMobile: '', //获取短信的手机号，防止获取短信后修改手机号
    showCountDown: false, //是否展示倒计时
    countDown: '', //60s倒计时
    sendButtonDisabled: false, //确定按钮
  },

  onLoad: function (options) {

  },
  // 获取手机号
  mobileChange(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  // 获取本机手机号  
  getPhoneNumber(e) {
    let that = this
    wx.login({
      async success(res) {
        const res1 = await reqWxUserInfo({
          jsCode: res.code
        }) //获取信息
        if (res1.code !== 20000) return Toast(res1.message)
        let {
          row
        } = res1.data
        // 解密
        let appId = 'wxda69189aa728178c'
        let encryptedData = e.detail.encryptedData
        let iv = e.detail.iv
        let pc = new WXBizDataCrypt(appId, row.session_key)
        let data = pc.decryptData(encryptedData, iv)
        if (!data) return Toast("获取手机号码失败，请重试")
        let mobile = data.phoneNumber
        that.setData({
          mobile
        })
      },
    })
  },
  // 清除手机号
  clearMobile() {
    this.setData({
      mobile: ''
    })
  },
  // 获取短信
  smsCodeChange(e) {
    this.setData({
      smsCode: e.detail.value
    })
  },
  /**
   * @name: 点击获取短信
   * @msg:  mobile
   * @return {*} smsCode
   */
  async getSmsCodeClick() {
    if (this.data.showCountDown) return
    let {
      mobile
    } = this.data
    if (!validateMobile(mobile)) return
    const res = await reqSmsCode({
      mobile
    })
    if (res.code != 20000) return Toast(res.data.errorInfo);
    Toast("请注意查收手机短信验证码")
    this.setData({
      showCountDown: true,
      countDown: 60,
      smsCodeMobile: mobile
    })
    let intervalId = setInterval(() => {
      let {
        countDown
      } = this.data
      if (this.data.countDown > 0 && this.data.countDown <= 60) {
        countDown -= 1
        this.setData({
          countDown
        })
      } else {
        this.setData({
          showCountDown: false
        })
        clearInterval(intervalId);
        intervalId = null;
      }
    }, 1000);
  },
  // 清除短信验证码
  clearSmsCode() {
    this.setData({
      smsCode: ''
    })
  },
  // 获取pwd1
  pwdChange1(e) {
    this.setData({
      pwd1: e.detail.value
    })
  },
  // 清除密码1 
  clearPwd1() {
    this.setData({
      pwd1: ''
    })
  },
  // pwd1 密码显隐
  showPwd1() {
    this.setData({
      isPassword1: !this.data.isPassword1
    })
  },
  // 获取pwd2
  pwdChange2(e) {
    this.setData({
      pwd2: e.detail.value
    })
  },
  // 清除密码2
  clearPwd2() {
    this.setData({
      pwd2: ''
    })
  },
  // pwd2 密码显隐
  showPwd2() {
    this.setData({
      isPassword2: !this.data.isPassword2
    })
  },


  /**
   * @name: 确定找回密码
   * @msg: 返回登录
   * @return {*}
   */
  async pwdConfirm() {
    let {
      mobile,
      smsCode,
      pwd1,
      pwd2,
      smsCodeMobile,
      sendButtonDisabled
    } = this.data
    if (sendButtonDisabled) return
    if (!smsCodeMobile) return Toast("请先通过手机号获取短信验证码")
    if (smsCodeMobile != mobile) return Toast("手机号与获取短信手机号不一致")
    if (!validatePwd(pwd1)) return
    if (pwd1 !== pwd2) return Toast("两次密码不一样")
    const res = await reqForgetPwd({
      mobile,
      code: smsCode,
      passWord: pwd1,
      verifyPassWord: pwd2,
    })
    console.log(res);
    if (res.code != 20000) return Toast(res.message);
    Toast.success("成功")
    this.setData({
      sendButtonDisabled: true
    })
    setTimeout(() => {
      this.setData({
        sendButtonDisabled: false
      })
      wx.navigateBack({
        delta: 1,
      })
    }, 1500)
  },

  backLogin() {
    wx.navigateBack({
      delta: 1,
    })
  }

})